package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Track;
import com.xmy.cultivate.entity.TrackDetail;
import com.xmy.cultivate.mapper.TrackDetailMapper;
import com.xmy.cultivate.mapper.TrackMapper;
import com.xmy.cultivate.service.ITrackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author min
 * @since 2022-12-27
 */
@Service
public class TrackServiceImpl extends ServiceImpl<TrackMapper, Track> implements ITrackService {

    @Autowired
    TrackDetailMapper trackDetailMapper;

    @Autowired
    TrackMapper trackMapper;

    @Transactional
    public boolean addBatchSourse(Track track){

        //System.out.println("track");
        //System.out.println(track);

        Track trackInfo = trackMapper.selectById(track.getId());

        for (Long sourseId:track.getSourseIdList()){
            QueryWrapper<TrackDetail> trackDetailQueryWrapper = new QueryWrapper<>();
            trackDetailQueryWrapper.eq("track_id",track.getId());
            trackDetailQueryWrapper.eq("sourse_id",sourseId);
            trackDetailQueryWrapper.eq("deleted",0);
            trackDetailQueryWrapper.eq("type",track.getType());
            Long count = trackDetailMapper.selectCount(trackDetailQueryWrapper);

            if(count.equals(0L)){
                TrackDetail trackDetail = new TrackDetail();
                trackDetail.setTrackId(track.getId());
                trackDetail.setType(track.getType());
                trackDetail.setSourseId(sourseId);
                trackDetail.setClassify(trackInfo.getClassify());
                trackDetailMapper.insert(trackDetail);
            }
        }
        return true;
    }
}
